/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React from 'react';
import { Form, message, Space, Input, InputNumber } from 'antd';
import type { ActionType } from '@ant-design/pro-table';
import { setDelivery } from '@/services/jdk/register';
import { AccountType, getPriceStorage, getRatioStorage } from '../../_utils';
import { DrawerForm } from '@ant-design/pro-form';
import styles from './index.less';

interface MarkupConfig {
  price: number;
  ratio: number;
  rario1: number;
  brand: string;
  brand_zh: string;
}

interface RefuseModalProps {
  children: JSX.Element;
  actionRef: ActionType | any;
  record: MarkupConfig[];
  user_id: string;
  league_id: string;
  formData: { [name: string]: any };
  type: AccountType;
}

const { Item } = Form;
const Index: React.FC<RefuseModalProps> = (props) => {
  const { record, children, actionRef, league_id, user_id, type } = props;
  const onFinish = async (values: { markup_config: MarkupConfig[] }) => {
    const { markup_config: list = [] } = values;
    const markup_config = list.map((item) => ({
      price: getPriceStorage(item.price),
      ratio: getRatioStorage(item.ratio),
      brand: item.brand,
    }));
    const { code, msg } = await setDelivery({
      user_id,
      league_id,
      type,
      markup_config: JSON.stringify(markup_config),
    });
    if (code == 0) {
      message.success('操作成功');
      actionRef.current?.reload();
      return true;
    } else {
      message.error(msg);
      return false;
    }
  };
  return (
    <DrawerForm
      title='加价设置'
      className={styles.KbPlatSetPriceModal}
      trigger={children}
      width={800}
      drawerProps={{
        destroyOnClose: true,
      }}
      layout='horizontal'
      labelCol={{ span: 8 }}
      rowProps={{ gutter: [12, 12] }}
      onFinish={onFinish}
      initialValues={{
        markup_config: record,
      }}
    >
      <Form.List name='markup_config'>
        {(fields) => (
          <>
            {fields.map(({ key, name, ...restField }) => (
              <Space key={key} style={{ display: 'flex', marginBottom: 8 }}>
                <Item {...restField} name={[name, 'brand']} style={{ display: 'none' }}>
                  <Input />
                </Item>
                <Item {...restField} label={record[key].brand_zh} name={[name, 'price']}>
                  <InputNumber
                    placeholder='输入价格'
                    precision={2}
                    min={0}
                    max={20}
                    style={{ width: 140 }}
                  />
                </Item>
                <Item
                  {...restField}
                  label='元/单，佣金比例'
                  name={[name, 'ratio']}
                  labelCol={{ span: 9 }}
                >
                  <InputNumber
                    addonBefore='服务商'
                    min={0}
                    max={10}
                    precision={1}
                    placeholder='输入比例'
                    style={{ width: '100%' }}
                  />
                </Item>
                <Item> : </Item>
                <Item {...restField} name={[name, 'ratio1']}>
                  <InputNumber
                    addonBefore='平台'
                    min={0}
                    max={10}
                    precision={1}
                    placeholder='输入比例'
                    style={{ width: 140 }}
                  />
                </Item>
              </Space>
            ))}
          </>
        )}
      </Form.List>
    </DrawerForm>
  );
};

export default Index;
